package com.gcosc.hly.codejava.controller;

import com.gcosc.hly.codejava.Utils.RequestContextUtils;
import com.gcosc.hly.codejava.entity.User;
import com.gcosc.hly.codejava.result.Result;
import com.gcosc.hly.codejava.service.LoginService;
import com.gcosc.hly.codejava.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;

/**
 * 其他
 * */
@RestController
@RequestMapping("/api/other")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OtherController {

    private final UserService userService;
    private final LoginService loginService;

    @GetMapping("/token")
    public Result detail(){
        return Result.ok(RequestContextUtils.getUser());
    }

    @GetMapping("/userInfo")
    public Result userInfo(){
        return Result.ok(userService.getUserInfoVO(RequestContextUtils.getUser().getId()));
    }


    /**
     * 登录
     * */
    @PostMapping("/login")
    public Result login(@RequestBody User params,
                        HttpServletResponse response){
        response.setHeader("Access-Control-Expose-Headers" , "token");
        response.setHeader("token", loginService.login(params));
        return Result.ok();
    }

}
